/* Copyright (C) 2008  Romolo Camplani, Marco Fortunato, Marco Marelli, Guido Rota, Fabio A. Schreiber et al.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * 
 */
package org.dei.perla.parser.expressions;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;

import org.dei.perla.parser.util.PairValue;

/**
 * Classe del repository di Funzioni
 * @author Stefano Vettor
 */
public class FunctionRepository {
	
	private ArrayList<Function> functionList;
	
	public FunctionRepository() {
		super();
		functionList = new ArrayList<Function>();
	}
	
	public Function getFunctionAtIndex(int i) {
		return functionList.get(i);
	}
	
	public void initialize(String fileURL) throws Exception {
		
		FileInputStream input = new FileInputStream(fileURL);
		XMLInputFactory factory = XMLInputFactory.newInstance();
		XMLStreamReader parser = factory.createXMLStreamReader(input);
		String element = null;
		
		boolean start_element = false;
		boolean newFunc = false;
		Function func = null;
		String parName = null;
		
		while(true)
		{
			int event = parser.next();
		    if (event == XMLStreamConstants.END_DOCUMENT) {
		       //System.out.println("END_DOCUMENT (Event" + event + ")");
		       parser.close();
		       break;
		    }
		    if (event == XMLStreamConstants.START_ELEMENT) {
		    	//System.out.println("START_ELEMENT - " + parser.getLocalName());
		    	element = parser.getLocalName();
		    	start_element = true;
		    	if(element.equals("function")) {
		    		newFunc = true;
		    		func = new Function();
		    	}
		    	
		    }
		    if (event == XMLStreamConstants.CDATA) {
		    	//System.out.println(" CDATA");
		    }
		    if (event == XMLStreamConstants.ATTRIBUTE) {
		    	//System.out.println(" ATTRIBUTE");
		    }
		    if (event == XMLStreamConstants.END_ELEMENT) {
		    	//System.out.println("END_ELEMENT - " + parser.getLocalName());
		    	element = parser.getLocalName();
		    	start_element = false;
		    	if(element.equals("function"))
		    	{
		    		this.functionList.add(func);
		    		newFunc = false;
		    	}
		    }
		    if (event == XMLStreamConstants.CHARACTERS) {
		    	if( newFunc && start_element)
		    	{
			    	if (element.equals("name")) func.setFunctionName(parser.getText());
			    	else if (element.equals("parName")) parName = parser.getText();
			    	else if (element.equals("parType")) {
			    		PairValue<String, Class<? extends Constant>> par = new PairValue<String, Class<? extends Constant>>();
			    		par.setFirst(parName);
			    		par.setSecond((Class<? extends Constant>) Class.forName(parser.getText()));
			    		//System.out.println(par.toString());
			    		func.addParameter(par);
			    	}
			    	else if (element.equals("returnedType")) func.setReturnedType((Class<? extends Constant>) Class.forName(parser.getText()));
		    	}

		    }
		    if (event == XMLStreamConstants.COMMENT) {
		    	//System.out.println("COMMENT");
		    }
		    if (event == XMLStreamConstants.ENTITY_DECLARATION) {
		    	//System.out.println(" ENTITY_DECLARATION");
		    }
		    if (event == XMLStreamConstants.ENTITY_REFERENCE) {
		    	//System.out.println(" ENTITY_REFERENCE");
		    }
		    if (event == XMLStreamConstants.SPACE) {
		    	//System.out.println("SPACE");
		    }
		}
		
		
	}
		
}
